package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.Prod;
import cn.com.dcsgo.dto.PageDTO;
import cn.com.dcsgo.dto.ProdAddDTO;
import cn.com.dcsgo.dto.ProdQueryDTO;
import cn.com.dcsgo.dto.ProdUpdateDTO;
import cn.com.dcsgo.model.Result;
import cn.com.dcsgo.service.ProdService;
import cn.com.dcsgo.vo.ProdVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 商品相关接口
 *
 * @author Dcsgo
 * @since 2025/10/19 9:20
 */
@Tag(name = "商品相关接口")
@RestController
@RequestMapping("prod/prod")
public class ProdController {
    @Resource
    private ProdService prodService;

    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    @Operation(summary = "分页查询商品信息")
    public Result<Page<Prod>> getProdPage(PageDTO pageDTO, ProdQueryDTO prodQueryDTO) {
        return Result.success(prodService.getPage(pageDTO, prodQueryDTO));
    }

    @DeleteMapping("{prodIds}")
    @PreAuthorize("hasAuthority('prod:prod:delete')")
    @Operation(summary = "逻辑删除商品", description = "支持批量删除，多个商品ID用‘,’分隔。实际是修改商品的状态为-1")
    public Result<Void> deleteProd(@PathVariable List<Long> prodIds) {
        return Result.handle(prodService.logicalRemoveBatchByIds(prodIds));
    }

    @PostMapping
    @PreAuthorize("hasAuthority('prod:prod:save')")
    @Operation(summary = "添加商品")
    public Result<Void> addProd(@Validated @RequestBody ProdAddDTO prodAddDTO) {
        return Result.handle(prodService.save(prodAddDTO));
    }

    @GetMapping("info/{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:info')")
    @Operation(summary = "获取商品信息")
    public Result<ProdVO> getProd(@PathVariable Long prodId) {
        return Result.success(prodService.getProdVOById(prodId));
    }

    @GetMapping("{prodId}")
    @Operation(summary = "获取商品基本信息", description = "用于远程调用")
    public Result<Prod> getBasicProdInfo(@PathVariable Long prodId) {
        return Result.success(prodService.getById(prodId));
    }

    @GetMapping("prod/prodInfo")
    @Operation(summary = "获取商品信息（不包含活动信息）")
    public Result<ProdVO> getProdInfo(@RequestParam Long prodId) {
        return Result.success(prodService.getProdAndSkuInfoById(prodId));
    }

    @GetMapping("list")
    @Operation(summary = "根据ID列表获取商品基本信息")
    public List<Prod> getBasicProdInfoByIds(@RequestParam("prodId") Set<Long> prodIds) {
        return prodService.listByIds(prodIds);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('prod:prod:update')")
    @Operation(summary = "修改商品信息")
    public Result<Void> updateProd(@Validated @RequestBody ProdUpdateDTO prodUpdateDTO) {
        return Result.handle(prodService.update(prodUpdateDTO));
    }
}
